#include "requirement_info_table.h"
#include "sqlite_manager.h"
#include "easylogging++.h"

RequirementInfoTable::RequirementInfoTable()
{

}

bool RequirementInfoTable::selectAllByPrj(
        QString prjUuid,
        QList<QVariantList> &data)
{
    SqliteManager sm;
    QList<QString> fields;
    fields << "uuid" << "parent_requirement_uuid" << "requirement_name" << "requirement_description";

    bool ret = sm.selectData("requirement_info", fields, {{"prj_uuid", prjUuid}}, data);
    if (!ret)
    {
        LOG(ERROR) << "init prj task data failed, error info: "
                   << sm.lastError().toStdString();
        return false;
    }

    return true;
}

bool RequirementInfoTable::insertNewRequirement(
        QString uuid,
        QString prjUuid,
        QString parentRequirementUuid,
        QString requirementName,
        QString requirementDescription,
        QString createTime,
        QString lastModifyTime,
        QString requirementStatus)
{
    QList<QString> fields;
    fields << "uuid"
           << "prj_uuid"
           << "parent_requirement_uuid"
           << "requirement_name"
           << "requirement_description"
           << "create_time"
           << "last_modify_time"
           << "requirement_status";
    QVariantList data;
    data << uuid
         << prjUuid
         << parentRequirementUuid
         << requirementName
         << requirementDescription
         << createTime
         << lastModifyTime
         << requirementStatus;
    SqliteManager sm;
    bool ret = sm.insertRowData("requirement_info", fields, data);
    if (!ret)
    {
        LOG(ERROR) << "insert row data failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}

bool RequirementInfoTable::updateRequirement(
        QString uuid,
        QString parentRequirementUuid,
        QString requirementName,
        QString requirementDescription,
        QString lastModifyTime,
        QString requirementStatus)
{
    QHash<QString, QVariant> data;
    data["parent_requirement_uuid"] = parentRequirementUuid;
    data["requirement_name"] = requirementName;
    data["requirement_description"] = requirementDescription;
    data["last_modify_time"] = lastModifyTime;
    data["requirement_status"] = requirementStatus;

    QHash<QString, QVariant> whereConditions;
    whereConditions["uuid"] = uuid;
    SqliteManager sm;
    bool ret = sm.updateData("requirement_info", data, whereConditions);
    if (!ret)
    {
        LOG(ERROR) << "update row data failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}

bool RequirementInfoTable::updateRequirementParentUuid(QString uuid, QString parentUuid)
{
    SqliteManager sm;
    bool ret = sm.updateData("requirement_info",
                             {
                                 {
                                     "parent_requirement_uuid", parentUuid
                                 }
                             },
                             {
                                 {
                                     "uuid", uuid
                                 }
                             });
    if (!ret)
    {
        LOG(ERROR) << "update requirement's parent uuid failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}

bool RequirementInfoTable::updateRequirementStatus(QString uuid, QString status)
{
    SqliteManager sm;
    bool ret = sm.updateData("requirement_info",
                             {
                                 {
                                     "requirement_status", status
                                 }
                             },
                             {
                                 {
                                     "uuid", uuid
                                 }
                             });
    if (!ret)
    {
        LOG(ERROR) << "update requirement's status failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}

bool RequirementInfoTable::deleteRequirement(QString uuid)
{
    QHash<QString, QVariant> whereConditions;
    whereConditions["uuid"] = uuid;
    SqliteManager sm;
    int ret = sm.deleteData("requirement_info", whereConditions);
    if (!ret)
    {
        LOG(ERROR) << "delete task failed, error info: "
                   << sm.lastError().toStdString();
    }
    return ret;
}

bool RequirementInfoTable::deleteChildRequirement(QString parentUuid)
{
    QHash<QString, QVariant> whereConditions;
    whereConditions["parent_requirement_uuid"] = parentUuid;
    SqliteManager sm;
    int ret = sm.deleteData("requirement_info", whereConditions);
    if (!ret)
    {
        LOG(ERROR) << "delete child task failed, error info: "
                   << sm.lastError().toStdString();
    }
    return ret;
}
